package cn.sanli.manage.controller;

import cn.sanli.manage.mapper.data1.UserMapper;
import cn.sanli.manage.mapper.data2.InformationMapper;
import cn.sanli.manage.security.LoginPrincipal;
import cn.sanli.manage.web.Encryption;
import cn.sanli.manage.web.JsonResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.annotation.Post;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/test")
//@PreAuthorize("hasAuthority('1')")
@Api(tags = "测试接口")
public class TestController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InformationMapper informationMapper;


    @GetMapping("/list1")
    @ApiOperation(value = "测试权限")
    //@PreAuthorize("hasAuthority('1')")
    @PreAuthorize("hasAnyAuthority('1','2','3','4')")
    public String list1(){
        System.out.println("1111111111");
       return "测试成功";
    }

    @GetMapping("/list2")
    @ApiOperation(value = "测试jwt登录")
    public JsonResult<String> list2(){
        System.out.println("2222");
        return JsonResult.ok("sdwcdscdsacnjksacbnhsdajbchjsadbchjdsabvchfdsajbvchjfdsabvhjf");
    }

    @GetMapping("/mapper1")
    @ApiOperation(value = "测试mapper1")
    public int mapper1(){
        int i = userMapper.selectUser1();
        return i;
    }

    @GetMapping("/mapper2")
    @ApiOperation(value = "测试mapper2")
    public int mapper2(){
        int i = informationMapper.selectUser1();
        return i;
    }

    Encryption encryption=new Encryption();
    String res;
    @GetMapping("/encryption")
    @ApiOperation(value = "密码加密")
    public String encryption(){
        res = encryption.base64Encrypt("123456");
        System.out.println(res);
        return "完成";
    }

    @GetMapping("/cryptanalysis")
    @ApiOperation(value = "密码解析")
    public String cryptanalysis(){
        String s = encryption.base64Decrypt("MTIzNDU2");
        System.out.println(s);
        return "完成";
    }

    @GetMapping("/shuzu")
    @ApiOperation(value = "数组接收Get请求")
    public String ceshi(Integer[] a){
        System.out.println(a);
        for(Integer b:a){
            System.out.println("传入的数组参数为"+b);
        }
        return "完成";
    }

    @PostMapping("/shuzu1")
    @ApiOperation(value = "数组接收post请求")
    public String ceshi1(@RequestBody Integer[] a){
        System.out.println(a);
        for(Integer b:a){
            System.out.println("传入的数组参数为"+b);
        }
        return "完成";
    }

    @GetMapping ("/shuzu2")
    @ApiOperation(value = "数组接收RequestParam请求")
    public String ceshi2(@RequestParam Integer[] a){
        System.out.println(a);
        for(Integer b:a){
            System.out.println("传入的数组参数为"+b);
        }
        return "完成";
    }

    @GetMapping("/select")
    @ApiOperation(value = "查询登录后当事人信息")
    public String select(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("登录后当事人信息为：{}",loginPrincipal);
        String userName = loginPrincipal.getUserName();
        System.out.println(userName);
        String roles = loginPrincipal.getRoles();
        log.debug("查询到的权限信息为{}",roles);
        List<String> role= JSON.parseArray(roles, String.class);
        for (String a:role){
            System.out.println(a);
            JSONObject json = JSONObject.parseObject(a);
            System.out.println("json对象为"+json);
            String authority = json.getString("authority");
            System.out.println(authority);
        }
        return "完成";
    }

}
